package com.aree.caipiao.shsq.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.aree.caipiao.shsq.dao.IShuangSeQiuResultDao;
import com.aree.caipiao.shsq.dao.jdbc.ShuangSeQiuResultDao;
import com.aree.caipiao.shsq.service.AnalizyService;

/**
 * 数据分析的实现类
 * @author hp-pc
 *
 */
public class AnalizyServiceImpl implements AnalizyService {
	private Log log = LogFactory.getLog(AnalizyServiceImpl.class);
	
	@Autowired
	private IShuangSeQiuResultDao shuangSeQiuResultDao;
	
	public AnalizyServiceImpl() {}
	
	public AnalizyServiceImpl(IShuangSeQiuResultDao shuangSeQiuResultDao) {
		this.shuangSeQiuResultDao = shuangSeQiuResultDao;
	}

	/* (non-Javadoc)
	 * @see com.aree.caipiao.shsq.service.AnalizyService#hezhiFenxi()
	 */
	public Map<Integer, Integer> hezhiFenxi() {
		List<Integer> results = shuangSeQiuResultDao.getHezhiTongji();
		log.debug("size=" + results.size());
		Map<Integer, Integer> fenxiResults = initHezhiFenxi();
		for (Integer result : results) {
			int storedTotal = fenxiResults.get(result);
			fenxiResults.put(result, ++storedTotal);
		}
		return fenxiResults;
	}
	
	private Map<Integer, Integer> initHezhiFenxi() {
		Map<Integer, Integer> fenxiResults = new TreeMap<Integer, Integer>();
		for (int i = 22; i <= 199; i++) {
			fenxiResults.put(i, 0);
		}
		return fenxiResults;
	}
	
	public static void main(String[] args) {
		ShuangSeQiuResultDao shuangSeQiuResultDao = new ShuangSeQiuResultDao();
		AnalizyService service = new AnalizyServiceImpl(shuangSeQiuResultDao);
		Map<Integer, Integer> result = service.hezhiFenxi();
		int total = shuangSeQiuResultDao.count();
		Map<Integer, Double> stardardes = service.standardHeZhiDispute();
		List<String> hezhis = new ArrayList<String>();
		List<String> shijis = new ArrayList<String>();
		List<String> liluns = new ArrayList<String>();
		DecimalFormat df = new DecimalFormat("0.000000");
		for (Integer hezhi : result.keySet()) {
			
			//System.out.println(hezhi + "\t:" + result.get(hezhi) + "\t" + (result.get(hezhi)*1.0/total *100) + "%");
			double standard = stardardes.get(hezhi) * 100;
			double acture = result.get(hezhi)*1.0/total * 100;
			double piancha = (acture - standard)/standard * 100;
			hezhis.add("'" + hezhi + "'");
			shijis.add(df.format(acture));
			liluns.add(df.format(standard));
			//System.out.printf("%d\t%d\t%.2f\t%.2f\t%5.2f", hezhi, result.get(hezhi), acture, standard, piancha);
			System.out.printf("%d\t%d\t%f\t%f\t%f", hezhi, result.get(hezhi), acture, standard, piancha);
			System.out.println();
			//System.out.printf("%d \t %5.2f% \t %5.2f%", hezhi, (result.get(hezhi)*1.0/total *100), stardardes.get(hezhi) *100);
		}
		System.out.println(JSONArray.fromObject(hezhis));
		System.out.println(JSONArray.fromObject(shijis));
		System.out.println(JSONArray.fromObject(liluns));
	}

	/* (non-Javadoc)
	 * @see com.aree.caipiao.shsq.service.AnalizyService#standardHeZhiDispute()
	 */
	public Map<Integer, Double> standardHeZhiDispute() {
		double total = 17721088d;
		Map<Integer, Integer> fenxiResults = initHezhiFenxi();
		for (int red1=1; red1 <=28; red1++) {
			for (int red2=red1+1; red2<=29; red2++) {
				for (int red3=red2+1; red3<=30; red3++) {
					for (int red4=red3+1; red4<=31; red4++) {
						for (int red5=red4+1; red5<=32; red5++) {
							for (int red6=red5+1; red6<=33; red6++) {
								for (int blue=1; blue<=16; blue++) {
									int he = red1 + red2 + red3 + red4 + red5 + red6 + blue;
									int storedTotal = fenxiResults.get(he);
									fenxiResults.put(he, ++storedTotal);
								}
							}
						}
					}
				}
			}
		}
		Map<Integer, Double> result = new TreeMap<Integer, Double>();
		double temp = 0;
		for (Integer he : fenxiResults.keySet()) {
			int num = fenxiResults.get(he);
			temp += num;
			result.put(he, num/total);
		}
		if (temp != total) System.out.println("计算有误" + temp + ":" + total);
		return result;
	}

	public IShuangSeQiuResultDao getShuangSeQiuResultDao() {
		return shuangSeQiuResultDao;
	}

	public void setShuangSeQiuResultDao(IShuangSeQiuResultDao shuangSeQiuResultDao) {
		this.shuangSeQiuResultDao = shuangSeQiuResultDao;
	}
}
